import App from './App'
import api from './api/index.js'
import config from './common/config.js'
import store from './store';
import utils from './common/utils.js'
import uView from '@/uni_modules/uview-ui'
Vue.use(uView);
/**
 * 提示信息
 * 
 * @param {Object} title 提示标题
 * @param {Object} duration 提示时间
 * @param {Object} mask 遮罩层
 * @param {Object} icon 图标层
 */
const msg = (title, duration = 1500, mask = false, icon = 'none') => {
	//统一提示方便全局修改
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}

/**
 * 页面跳转
 * @param {Object} url 需要跳转的应用内非 tabBar 的页面的路径
 * @param {Object} animationType 窗口显示的动画效果
 * @param {Object} animationDuration 窗口动画持续时间，单位为 ms
 */
const to = (url, animationType = 'pop-in', animationDuration = 300, needLogin = false) => {
	uni.navigateTo({
		url,
		animationType,
		animationDuration,
		success: function(res) {
			config.debug ? console.log(res) : '';
		},
		fail: function(e) {
			config.debug ? console.log(e) : '';
		}
	})
}

/**
 * 图像处理
 * @param {Object} url 图像地址
 * @param {Object} h  高度
 * @param {Object} w  宽度  
 * @param {Object} modenum 1.自适应 2.固定宽高短边缩放 3.固定宽高居中裁剪 4.强制宽高
 * @param {Object} type  使用类型  
 * @param {Object} format  图形格式  
 * @param {Object} resize  resize  
 * @param {Object} multiple  放大倍数  
 * 
 * $wanlshop.oss(url, 120, 0, 2, 'transparent', 'png')
 */
const oss = (url, w = 120, h = 120, modenum = 2, type = '', format = 'jpg', resize = 'resize', multiple = 3) => {
	let image = '';
	let mode = ["m_lfit", "m_mfit", "m_fill", "m_fixed"];
	// 图像，自适应方向：1，渐进显示：1，转换格式：jpg，图片质量：q_90，图片锐化：50，浓缩图
	let rule = ["?x-oss-process=image", "auto-orient,1", "interlace,1", "format,jpg", "quality,q_90", "sharpen,50",
		"resize,m_fixed,w_120,h_120"
	];
	// 转换格式
	if (format == 'png') {
		rule[3] = ["format", "png"].join(",");
	}
	// 判断是否有高度
	if (w == 0) {
		rule[6] = [resize, mode[modenum], `h_${h * multiple}`].join(",");
	} else if (h == 0) {
		rule[6] = [resize, mode[modenum], `w_${w * multiple}`].join(",");
	} else {
		rule[6] = [resize, mode[modenum], `w_${w * multiple}`, `h_${h * multiple}`].join(",");
	}
	//当地址不存在
	if (url) {
		if ((/^data:image\//.test(url))) {
			image = url;
		} else if ((/^(http|https):\/\/.+/.test(url))) {
			rule.pop();
			image = url + rule.join("/");
		} else {
			image = config.url_config + url + rule.join("/");
		}
	}/* else {
		if (type == 'transparent') {

		} else if (type == 'avatar') {
			image = appstc('/common/mine_def_touxiang_3x.png');
		} else {
			image = appstc('/common/img_default3x.png');
		}
	} */
	return image;
}

/**
 * 获取系统信息
 */
const wanlsys = () => {
	let sys = uni.getSystemInfoSync();
	let data = {
		top: sys.statusBarHeight,
		height: sys.statusBarHeight + uni.upx2px(90),
		screenHeight: sys.screenHeight,
		platform: sys.platform,
		model: sys.model,
		windowHeight: sys.windowHeight,
		windowBottom: sys.windowBottom
	};
	// #ifndef MP
	data.height = sys.statusBarHeight + uni.upx2px(90);
	// #endif
	// #ifdef MP-WEIXIN || MP-BAIDU || MP-QQ || MP-TOUTIAO || MP-WEIXIN
	let custom = uni.getMenuButtonBoundingClientRect();
	data.height = uni.upx2px(20) + custom.bottom + custom.top - sys.statusBarHeight;
	// #endif		
	// #ifdef MP-ALIPAY
	data.height = sys.statusBarHeight + sys.titleBarHeight;
	// #endif
	config.debug ? console.log(data) : '';
	return data;
}

/* 处理富文本中的图片 */
const rich = (richtext) => {
	const regex = new RegExp('<img', 'gi');
	return richtext.replace(/(\<img|\<p)/gi, function($0, $1) {
		return {
			"<img": '<img style="width:100%;height:auto;display:block;" ',
			"<p": '<p ',
			"<article": "<div",
			"</article": "</div",
			"<header": "<div",
			"</header": "</div"
		} [$1];
	});
}


/**
 * @param {Object} num 返回页面数量
 */
const back = (num = 1) => {
	uni.navigateBack({
		delta: num
	});
}

// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'

Vue.prototype.$config = config
Vue.prototype.$api = api
Vue.prototype.$store = store; //挂载在 Vue 实例上
Vue.prototype.$utils = utils
/* 跳珠到技师 */
Vue.prototype.goIndex = function(id) {
	uni.navigateTo({
		url: '/pages/index/index?id=' + id
	})
}


Vue.prototype.$tool = {
	msg,
	to,
	oss,
	back,
	rich,
	wanlsys
};

const app = new Vue({
    ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif